package com.tedu.openmedical.medicalsystem.base.exception;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.tedu.openmedical.base.response.JsonResult;
import com.tedu.openmedical.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalHandlerException {

    @ExceptionHandler(VerificationCodeErrorException.class)
    public JsonResult doHandleVerificationCodeErrorException(VerificationCodeErrorException ex) {
        log.error("VerificationCodeErrorException: {}", ex.getMessage());
        return new JsonResult(StatusCode.VERIFICATION_CODE_ERROR, ex.getMessage());
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public JsonResult incorrectCredentials(IncorrectCredentialsException e) {
        log.error("IncorrectCredentialsException: {}", e.getMessage());
        return new JsonResult(StatusCode.PASSWORD_ERROR, e.getMessage());
    }


    @ExceptionHandler(UsernameErrorException.class)
    public JsonResult doHandleUsernameErrorException(UsernameErrorException ex) {
        System.out.println("捕获成功");
        String message = ex.getMessage();
        return new JsonResult(StatusCode.USERNAME_ERROR,message);
    }

    @ExceptionHandler(ShiroException.class)
    public JsonResult doHandleShiroException(ShiroException ex) {
        String message = ex.getMessage();
        return new JsonResult(StatusCode.UNAUTHORIZED,message);
    }

    @ExceptionHandler(UnauthenticatedException.class)
    public JsonResult doHandleUnauthenticatedException(UnauthenticatedException ex) {
        String message = ex.getMessage();
        return new JsonResult(StatusCode.UNAUTHORIZED,message);
    }

    @ExceptionHandler(TokenExpiredException.class)
    public JsonResult doHandleTokenExpiredException(TokenExpiredException ex) {
        String message = ex.getMessage();
        return new JsonResult(StatusCode.TOKEN_VERIFY_FAIL,message);
    }
}
